﻿using System;

namespace Gamelight.Sound
{
	public enum MusicLoopStyle
    {
		PlayOnce,
		LoopForever
	}

	public enum MusicTransitionStyle
    {
		Instant,
		FadeOutCurrentAndPlay,
		FadeOutCurrentAndStop,
		FadeOutCurrentFadeInThisSimultaneously
	}

	public class Music
	{
		public static Music DefaultFade = new Music(null) { TransitionSeconds = 0.3, Transition = MusicTransitionStyle.FadeOutCurrentAndStop };

		public Music(string assembly, string path)
		{
			if (!string.IsNullOrEmpty(path))
			{
				this.InternalSound = new Sound(path);
			}
			this.Transition = MusicTransitionStyle.Instant;
			this.VolumePercentage = 100;
			this.Loop = MusicLoopStyle.PlayOnce;
			this.TransitionLength = TimeSpan.Zero;
		}

		public Music(string path) : this(null, path) { }

		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		public MusicTransitionStyle Transition { get; set; }
		internal TimeSpan TransitionLength { get; set; }

		public double TransitionSeconds
		{
			get { return this.TransitionLength.TotalMilliseconds / 1000; }
			set { this.TransitionLength = new TimeSpan((long)(value * 10000000)); }
		}

		public MusicLoopStyle Loop { get; set; }
		public double NormalizationFactor
		{
			get { return this.VolumePercentage / 100.0; }
			set { this.VolumePercentage = (int)(value * 100); }
		}

        internal Gamelight.Sound.Sound InternalSound { get; set; }
		internal int VolumePercentage { get; set; }


		public override bool Equals(object obj)
		{
			Music other = obj as Music;
			if (other != null)
			{
				if (this.InternalSound == null && other.InternalSound == null)
				{
					return true;
				}
				else if (this.InternalSound == null && other.InternalSound != null)
				{
					return false;
				}
				else if (this.InternalSound != null && other.InternalSound == null)
				{
					return false;
				}

				return this.InternalSound.Matches(other.InternalSound);
			}
			return false;
		}
	}
}
